package com.example.springboot.controller;

import com.example.springboot.mapper.UserResponsitory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class BasicController {

    protected Logger logger = LoggerFactory.getLogger(BasicController.class);

    @Autowired
    protected UserResponsitory userResponsitory;

    @Autowired
    protected BCryptPasswordEncoder bCryptPasswordEncoder;

    public List<String> getAuthentication() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        List<String> list = new ArrayList<>();
        for (GrantedAuthority grantedAuthority: authorities) {
            list.add(grantedAuthority.getAuthority());
        }
        return list;
    }
}
